/*
 * Copyright (c) Created by T C L on 2019.2.26.
 */

package com.egg.ylt.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.egg.ylt.R;
import com.egg.ylt.Utils.CustomUtils;
import com.egg.ylt.activity.ACT_ShopDetail;
import com.egg.ylt.pojo.ShopListEntity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ADA_StoreListAdapter extends RecyclerView.Adapter<ADA_StoreListAdapter.StoreListViewHolder> {


    private Context mContext;
    private List<ShopListEntity> mStoreList;

    public ADA_StoreListAdapter(List<ShopListEntity> storeList) {
        this.mStoreList = storeList;
        notifyDataSetChanged();
    }

    @Override
    public StoreListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        return new StoreListViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_store_list,
                parent, false));
    }

    @Override
    public void onBindViewHolder(StoreListViewHolder holder, int position) {
        holder.refreshData(position);
    }

    @Override
    public int getItemCount() {
        return mStoreList == null ? 0 : mStoreList.size();
    }

    class StoreListViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.store_name_tv)
        TextView mStoreNameTv;
        @BindView(R.id.store_address_tv)
        TextView mStoreAddressTv;
        @BindView(R.id.distance_tv)
        TextView mDistanceTv;
        @BindView(R.id.distance_tips_tv)
        TextView mDistanceTipsTv;
        @BindView(R.id.store_phone_iv)
        ImageView mStorePhoneIv;

        public StoreListViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void refreshData(int position) {
            final ShopListEntity dataEntity = mStoreList.get(position);

            mStoreNameTv.setText(dataEntity.getShopName());

            mStoreAddressTv.setText(dataEntity.getAddress());

            mDistanceTv.setText(dataEntity.getLength());

            mStorePhoneIv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    CustomUtils.setCallPhoneDialog(mContext,dataEntity.getFixPhone());
                }
            });


            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, ACT_ShopDetail.class);
                    intent.putExtra("id", dataEntity.getShopId());
                    mContext.startActivity(intent);
                }
            });
        }

    }
}
